/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ing.sisval.gui.seguridad;

import javax.swing.JOptionPane;
import org.ing.sisval.be.BEPersona;
import org.ing.sisval.bl.BLPersona;
import org.ing.sisval.gui.JFPrincipal;
import org.ing.sisval.gui.control.model.ModelPersona;
import org.rx.cr.gui.JInternalFrameUC;
import org.rx.cr.util.Utilitarios;
import static org.rx.cr.util.Utilitarios.alinearDatosColumnaTablaCentro;
import static org.rx.cr.util.Utilitarios.disableComponent;
import static org.rx.cr.util.Utilitarios.enableComponent;
import static org.rx.cr.util.Utilitarios.formateaAnchoColumnaTabla;
import static org.rx.cr.util.Utilitarios.resetComponents;

/**
 *
 * @author Mysham
 */
public class JIFAdministrarPersona extends JInternalFrameUC {

    JFPrincipal parent;
    BEPersona bePersona;
    BLPersona blPersonal;
    ModelPersona modelPersonalInformatico;

    /**
     * Creates new form JIFAdministrarPersonal
     *
     * @param parent
     */
    public JIFAdministrarPersona(JFPrincipal parent) {
        initComponents();
        Utilitarios.setIconoVentana(this, "/org/ing/sisval/imagen/app_icon.png");
        this.parent = parent;
        modelPersonalInformatico = new ModelPersona();
        jtPersonal.setModel(modelPersonalInformatico);
        blPersonal = new BLPersona(parent.getConf());
        //parent.deshabilitarMenu();
        _init();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        txfDni = new javax.swing.JTextField();
        jcbEstado = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txfNombre = new javax.swing.JTextField();
        txfApPaterno = new javax.swing.JTextField();
        txfApMaterno = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txfClave = new javax.swing.JPasswordField();
        jPanel1 = new javax.swing.JPanel();
        txfBuscar = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtPersonal = new javax.swing.JTable();
        jbEditar = new javax.swing.JButton();
        jbGuardar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbAgregar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DATOS DE USUARIO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jcbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleccionar>", "ACTIVO", "INACTIVO" }));

        jLabel1.setText("DNI :");

        jLabel2.setText("Clave :");

        jLabel3.setText("Estado :");

        txfNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfNombreActionPerformed(evt);
            }
        });

        txfApMaterno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfApMaternoActionPerformed(evt);
            }
        });

        jLabel4.setText("Nombres :");

        jLabel5.setText("Ap. Paterno :");

        jLabel6.setText("Ap. Materno :");

        txfClave.setText("jPasswordField1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txfNombre)
                    .addComponent(txfApPaterno)
                    .addComponent(txfApMaterno)
                    .addComponent(txfDni)
                    .addComponent(txfClave, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfApPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfApMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txfClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSCAR USUARIO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/small_buscar.png"))); // NOI18N
        jbBuscar.setBorder(null);

        jtPersonal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jtPersonal);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txfBuscar)
                    .addComponent(jbBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))
        );

        jbEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Modificar_Min.png"))); // NOI18N
        jbEditar.setText("EDITAR");
        jbEditar.setEnabled(false);
        jbEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEditar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEditarActionPerformed(evt);
            }
        });

        jbGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Guardar_Min.png"))); // NOI18N
        jbGuardar.setText("GUARDAR");
        jbGuardar.setEnabled(false);
        jbGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbGuardar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Cancelar_Min.png"))); // NOI18N
        jbCancelar.setText("CANCELAR");
        jbCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbCancelar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Nuevo.png"))); // NOI18N
        jbAgregar.setText("AGREGAR");
        jbAgregar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAgregar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jbAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                    .addComponent(jbEditar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                    .addComponent(jbGuardar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                    .addComponent(jbAgregar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEditarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbEditarActionPerformed

    private void txfNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfNombreActionPerformed

    private void txfApMaternoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfApMaternoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfApMaternoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbGuardar;
    private javax.swing.JComboBox jcbEstado;
    private javax.swing.JTable jtPersonal;
    private javax.swing.JTextField txfApMaterno;
    private javax.swing.JTextField txfApPaterno;
    private javax.swing.JTextField txfBuscar;
    private javax.swing.JPasswordField txfClave;
    private javax.swing.JTextField txfDni;
    private javax.swing.JTextField txfNombre;
    // End of variables declaration//GEN-END:variables

    @Override
    public void _cargarCombos() throws Exception {
    }

    @Override
    public void _agregaValidadoresControl() {
        Utilitarios.addEnterDoclickEvent(new Object[][]{{txfBuscar, jbBuscar}});
        _addOperationEventsButtons(jbAgregar, jbEditar, jbGuardar, jbCancelar);
        _addRestoreDataEventsTable(jtPersonal);
        _addSearchEventsButton(jbBuscar);
        Utilitarios.addCharacterValidatorEvent(new Object[][]{
            {txfNombre, Utilitarios.ALFABETICO, 180},
            {txfApPaterno, Utilitarios.ALFABETICO, 180},
            {txfApMaterno, Utilitarios.ALFABETICO, 180},
            {txfDni, Utilitarios.NUMERICO, 8}
        });
    }

    @Override
    public boolean _isDatosValidosBE() {
        return Utilitarios.isDatosLlenos(new Object[][]{
            {txfClave, "Ingresar la clave."},
            {jcbEstado, "Seleccionar el estado de la cuenta."},
            {txfDni, "Ingresar el dni."},
            {txfNombre, "Ingresar el nombre."},});
    }

    @Override
    public int _registrarBE() throws Exception {
        try {
            bePersona = new BEPersona();
            bePersona.setApmaterno(txfApMaterno.getText());
            bePersona.setAppaterno(txfApPaterno.getText());
            bePersona.setNombres(txfNombre.getText());
            bePersona.setDni(txfDni.getText());
            bePersona.setClave(txfClave.getText());
            bePersona.setEstadocuenta(jcbEstado.getSelectedItem().toString());
            int id = blPersonal.registrar(bePersona);

            _resetControls();
            _deshabilitarControlesDatos();
            _listarBEs();
            return id;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al registrar al usuario. " + e, "Usuario", JOptionPane.INFORMATION_MESSAGE);
            return -1;
        }
    }

    @Override
    public int _guardarCambiosBE() throws Exception {
        try {
            bePersona = modelPersonalInformatico.get(jtPersonal.getSelectedRow());
            bePersona.setApmaterno(txfApMaterno.getText());
            bePersona.setAppaterno(txfApPaterno.getText());
            bePersona.setNombres(txfNombre.getText());
            bePersona.setDni(txfDni.getText());
            bePersona.setClave(txfClave.getText());
            bePersona.setEstadocuenta(jcbEstado.getSelectedItem().toString());
            int rsp = blPersonal.actualizarRegistro(bePersona);

            _deshabilitarControlesDatos();
            _resetControls();
            _listarBEs();
            return rsp;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al actualizar al usuario. " + e, "Usuario", JOptionPane.INFORMATION_MESSAGE);
            return -1;

        }
    }

    @Override
    public void _listarBEs() throws Exception {
        modelPersonalInformatico.addAll(blPersonal.buscarReferencia(txfBuscar.getText()));
        personalizaVistaTabla();
    }

    @Override
    public void _recuperarDatosBE() throws Exception {
        try {
            if (jtPersonal.getSelectedRow() >= 0) {
                bePersona = modelPersonalInformatico.get(jtPersonal.getSelectedRow());
                txfNombre.setText(bePersona.getNombres());
                txfApPaterno.setText(bePersona.getAppaterno());
                txfApMaterno.setText(bePersona.getApmaterno());
                txfDni.setText(bePersona.getDni());
                txfClave.setText(bePersona.getClave());
                jcbEstado.setSelectedItem(bePersona.getEstadocuenta());
            }
            jbEditar.setEnabled(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al registrar al usuario. " + e, "Usuario", JOptionPane.INFORMATION_MESSAGE);

        }

    }

    @Override
    public void _deshabilitarControlesDatos() {
        disableComponent(new Object[]{
            txfNombre, txfApMaterno, txfApPaterno, txfDni, txfClave, jcbEstado
        });
    }

    @Override
    public void _habilitarControlesDatos() {
        enableComponent(new Object[]{
            txfNombre, txfApMaterno, txfApPaterno, txfDni, txfClave, jcbEstado
        });
    }

    @Override
    public void _resetControls() {
        resetComponents(new Object[]{
            txfNombre, txfApMaterno, txfApPaterno, txfDni, txfClave, jcbEstado
        });
    }

    private void personalizaVistaTabla() {
        alinearDatosColumnaTablaCentro(0, jtPersonal);
        alinearDatosColumnaTablaCentro(1, jtPersonal);

        formateaAnchoColumnaTabla(0, jtPersonal, 200);
        formateaAnchoColumnaTabla(1, jtPersonal, 80);

    }
}
